﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Globalization;

namespace CubaSite.Configuracion
{
    /// <summary>
    /// Summary description for LanguageUtilities
    /// </summary>
    public class LanguageUtilities
    {

        public LanguageUtilities()
        {
        }

        /// <summary>
        /// Determine the language needed from the HttpContext and set the language for the thread
        /// </summary>
        public static void SetLanguageFromContext()
        {
            string NewUICulture = "es";
            string NewCulture = "es-ES";

            string languageParam = GetLanguageFromContext();

            if (languageParam.ToUpper() == "ES")
            {
                NewUICulture = "es";
                NewCulture = "es-ES";
            }
            else if (languageParam.ToUpper() == "EN")
            {
                NewUICulture = "en";
                NewCulture = "en-US";
            }

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(NewCulture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(NewUICulture);

            if (HttpContext.Current.Request.Cookies["LanguageSettings"] == null)
            {
                // Creamos elemento HttpCookie con su nombre y su valor
                HttpCookie myCookie = new HttpCookie("LanguageSettings", NewUICulture);
                myCookie.Expires = DateTime.Now.AddDays(360);

                // Y finalmente añadimos la cookie a nuestro usuario
                HttpContext.Current.Response.Cookies.Add(myCookie);
            }
        }

        public static string GetLanguageFromContext()
        {
            //Valor por defecto
            string language = "es";

            //return language;

            //User has not specified a language explicitly.  
            //Try to see if we can REMEMBER the last language used
            if (HttpContext.Current.Request.Cookies["LanguageSettings"] != null)
            {
                HttpCookie myCookie = HttpContext.Current.Request.Cookies.Get("LanguageSettings");
                language = myCookie.Value;
            }
            else if (HttpContext.Current.Request.UserLanguages != null)
            {
                // Try to detect language from the Browser
                try
                {
                    string lang = HttpContext.Current.Request.UserLanguages[0];
                    if (lang.Substring(0, 2).ToUpper() == "ES")
                        language = "es";
                    else if (lang.Substring(0, 2).ToUpper() == "EN")
                        language = "en";
                }
                catch { }
            }

            return language;
        }

        public static void ChangeLanguageFromContext(string language)
        {
            string NewUICulture = "es";
            string NewCulture = "es-ES";

            if (language.ToUpper() == "ES")
            {
                NewUICulture = "es";
                NewCulture = "es-ES";
            }
            else if (language.ToUpper() == "EN")
            {
                NewUICulture = "en";
                NewCulture = "en-US";
            }

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(NewCulture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(NewUICulture);

            if (HttpContext.Current.Request.Cookies["LanguageSettings"] == null)
            {
                // Creamos elemento HttpCookie con su nombre y su valor
                HttpCookie myCookie = new HttpCookie("LanguageSettings", language);
                myCookie.Expires = DateTime.Now.AddDays(360);

                // Añadimos la cookie a nuestro usuario
                HttpContext.Current.Response.Cookies.Add(myCookie);
            }
            else
            {
                // Para actualizar los datos de la cookie
                HttpCookie myCookie = HttpContext.Current.Request.Cookies.Get("LanguageSettings");
                myCookie.Value = language;
                myCookie.Expires = DateTime.Now.AddDays(360);
                HttpContext.Current.Response.Cookies.Set(myCookie);
            }
        }
    }
}